//
//  ZBLargeImageBrowserViewCell.m
//  XZBProduct
//
//  Created by xzb on 2019/1/21.
//  Copyright © 2019 xzb. All rights reserved.
//

#import "ZBLargeImageBrowserViewCell.h"


NSString *const ZBLargeImageBrowserViewCellIdentifier = @"ZBLargeImageBrowserViewCell";

@implementation ZBLargeImageBrowserViewCellModel

- (NSString *)identify
{
    return ZBLargeImageBrowserViewCellIdentifier;
}

@end

@implementation ZBLargeImageBrowserViewCell

#pragma mark - life cycle

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubView];
        [self addLayout];
  
    }
    return self;
}

#pragma mark - BDCellInfoProtocol

- (void)loadData:(id)data
{
    if (![data isKindOfClass:[ZBLargeImageBrowserViewCellModel class]]) {
        return;
    }
    self.cellModel = data;
    
    [self handleContentscrollview];
    [self handleContentimgview];
    
    self.contentScrollView.contentOffset = CGPointMake(0, self.cellModel.contentOffsetY);    
}

+ (CGSize)itemSize:(ZBLargeImageBrowserViewCellModel *)data
{
    if (![data isKindOfClass:[ZBLargeImageBrowserViewCellModel class]]) {
        return CGSizeZero;
    }
    return CGSizeMake(CGRectGetWidth([UIScreen mainScreen].bounds), data.cellHeight);
}

#pragma mark - event Response

#pragma mark - private methods

- (void)addSubView
{
    [self.contentView addSubview:self.contentScrollView];
    [self.contentScrollView addSubview:self.contentImgView];
}

- (void)addLayout
{
    [self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.contentView);
        make.height.equalTo(self.contentView);
        make.top.left.equalTo(self.contentView);
    }];
    [self.contentImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)handleContentscrollview
{
    self.contentScrollView.contentSize = self.cellModel.image.size;
}

- (void)handleContentimgview
{
    self.contentImgView.image = self.cellModel.image;
    [self.contentImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(self.cellModel.image.size.width);
        make.height.offset(self.cellModel.image.size.height);
        make.top.left.equalTo(self.contentScrollView);
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self zb_event:@"ZBLargeImageBrowserView" params:@{
                                                       @"cell" : self,
                                                       @"contentOffsetY" : @(self.contentScrollView.contentOffset.y)
                                                       }];
}
#pragma mark - getter And setter

- (UIScrollView *)contentScrollView
{
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc] init];
        _contentScrollView.delegate = self;
    }
    return _contentScrollView;
}

- (UIImageView *)contentImgView
{
    if (!_contentImgView) {
        _contentImgView = [[UIImageView alloc] init];
    }
    return _contentImgView;
}

@end

